﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace iCPUApp.Code
{
    class Processor
    {

            private Dictionary<int, string> AbilityList = new Dictionary<int, string>();
            private Dictionary<int, string> CpuStatusList = new Dictionary<int, string>();
            private Dictionary<int, string> FamilyList = new Dictionary<int, string>();
            private Dictionary<int, string> ArchitectureList = new Dictionary<int, string>();
            private Dictionary<int, string> ProcessorTypeList = new Dictionary<int, string>();
            private Dictionary<int, string> VolatageCapList = new Dictionary<int, string>();
            private ManagementObjectSearcher mos;
            private ManagementObjectCollection moc;
            static ManagementBaseObject mo;

            public string GetStringProperty(string PropertyName)
            {   
               
                try
                {
                    return mo.Properties[PropertyName].Value.ToString();
                }
                catch
                {
                   
                }
                return "Value Not Available";
            }

            public Processor(string DeviceID)
            {
                try
                {
                    mos = new ManagementObjectSearcher("select * from Win32_Processor where DeviceID='" + DeviceID + "'");
                    moc = mos.Get();
                }
                catch
                {
                }

                try
                {

                    /* Architecture List */
                    ArchitectureList.Add(0,"x86");
                    ArchitectureList.Add(1,"MIPS");
                    ArchitectureList.Add(2,"Alpha");
                    ArchitectureList.Add(3,"PowerPC");
                    ArchitectureList.Add(6,"Intel Itanium Processor Family (IPF)");
                    ArchitectureList.Add(9,"x64");

                    /* Ability List */
                    AbilityList.Add(1, "Other");
                    AbilityList.Add(2, "Unknown");
                    AbilityList.Add(3, "Running or Full Power");
                    AbilityList.Add(4, "Warning");
                    AbilityList.Add(5, "In Test");
                    AbilityList.Add(6, "Not Applicable");
                    AbilityList.Add(7, "Power Off");
                    AbilityList.Add(8, "Off Line");
                    AbilityList.Add(9, "Off Duty");
                    AbilityList.Add(10, "Degraded");
                    AbilityList.Add(11, "Not Intalled");
                    AbilityList.Add(12, "Install Error");
                    AbilityList.Add(13, "Power Save ,exact Status Unknown");
                    AbilityList.Add(14, "Power Save - Low Power Mode");
                    AbilityList.Add(15, "Power Save - Standby");
                    AbilityList.Add(16, "Power Cycle");
                    AbilityList.Add(17, "Power Save - Warning");

                    /* CpuStatus List */
                    CpuStatusList.Add(0, "Unknown");
                    CpuStatusList.Add(1, "CPU Enabled");
                    CpuStatusList.Add(2, "CPU Disabled by User via BIOS Setup");
                    CpuStatusList.Add(3, "CPU Disabled by BIOS (POST Error)");
                    CpuStatusList.Add(4, "Cpu is Idle");
                    CpuStatusList.Add(5, "Reserved");
                    CpuStatusList.Add(6, "Reserved");
                    CpuStatusList.Add(7, "Other");

                    /*Family List */
                    FamilyList.Add(1, "Other");
                    FamilyList.Add(2, "Unknown");
                    FamilyList.Add(3, "8086");
                    FamilyList.Add(4, "80286");
                    FamilyList.Add(5, "Intel386 Processor");
                    FamilyList.Add(6, "Intel486 Processor");
                    FamilyList.Add(7, "8087");
                    FamilyList.Add(8, "80287");
                    FamilyList.Add(9, "80387");
                    FamilyList.Add(10, "80487");
                    FamilyList.Add(11, "Pentium Brand");
                    FamilyList.Add(12, "Pentium Pro");
                    FamilyList.Add(13, "Pentium II");
                    FamilyList.Add(14, "Pentium Processor with MMX Technology");
                    FamilyList.Add(15, "Celeron");
                    FamilyList.Add(16, "Pentium II Xeon");
                    FamilyList.Add(17, "Pentium III");
                    FamilyList.Add(18, "M1 Family");
                    FamilyList.Add(19, "M2 Family");
                    FamilyList.Add(24, "AMD Duron Processor Family");
                    FamilyList.Add(25, "K5 Family");
                    FamilyList.Add(26, "K6 Family");
                    FamilyList.Add(27, "K6-2");
                    FamilyList.Add(28, "K6-3");
                    FamilyList.Add(29, "AMD Athlon Processor Family");
                    FamilyList.Add(30, "AMD2900 Family");
                    FamilyList.Add(31, "K6-2+");
                    FamilyList.Add(32, "Power PC Family");
                    FamilyList.Add(33, "Power PC 601");
                    FamilyList.Add(34, "Power PC 603");
                    FamilyList.Add(35, "Power PC 603+");
                    FamilyList.Add(36, "Power PC 604");
                    FamilyList.Add(37, "Power PC 620");
                    FamilyList.Add(38, "Power PC X704");
                    FamilyList.Add(39, "Power PC 750");
                    FamilyList.Add(48, "Alpha Family");
                    FamilyList.Add(49, "Alpha 21064");
                    FamilyList.Add(50, "Alpha 21066");
                    FamilyList.Add(51, "Alpha 21164");
                    FamilyList.Add(52, "Alpha 21164PC");
                    FamilyList.Add(53, "Alpha 21164a");
                    FamilyList.Add(54, "Alpha 21264");
                    FamilyList.Add(55, "Alpha 21364");
                    FamilyList.Add(64, "MIPS Family");
                    FamilyList.Add(65, "MIPS R4000");
                    FamilyList.Add(66, "MIPS R4200");
                    FamilyList.Add(67, "MIPS R4400");
                    FamilyList.Add(68, "MIPS R4600");
                    FamilyList.Add(69, "MIPS R10000");
                    FamilyList.Add(80, "SPARC Family");
                    FamilyList.Add(81, "SuperSPARC");
                    FamilyList.Add(82, "microSPARC II");
                    FamilyList.Add(83, "microSPARC IIep");
                    FamilyList.Add(84, "UltraSPARC");
                    FamilyList.Add(85, "UltraSPARC II");
                    FamilyList.Add(86, "UltraSPARC IIi");
                    FamilyList.Add(87, "UltraSPARC III");
                    FamilyList.Add(88, "UltraSPARC IIIi");
                    FamilyList.Add(96, "68040");
                    FamilyList.Add(97, "68xxx Family");
                    FamilyList.Add(98, "68000");
                    FamilyList.Add(99, "68010");
                    FamilyList.Add(100, "68020");
                    FamilyList.Add(101, "68030");
                    FamilyList.Add(112, "Hobbit Family");
                    FamilyList.Add(120, "Crusoe TM5000 Family");
                    FamilyList.Add(121, "Crusoe TM3000 Family");
                    FamilyList.Add(122, "Efficeon TM8000 Family");
                    FamilyList.Add(128, "Weitek");
                    FamilyList.Add(130, "Itanium Processor");
                    FamilyList.Add(131, "AMD Athlon 64 Processor Famiily");
                    FamilyList.Add(132, "AMD Opteron Processor Family");
                    FamilyList.Add(144, "PA-RISC Family");
                    FamilyList.Add(145, "PA-RISC 8500");
                    FamilyList.Add(146, "PA-RISC 8000");
                    FamilyList.Add(147, "PA-RISC 7300LC");
                    FamilyList.Add(148, "PA-RISC 7200");
                    FamilyList.Add(149, "PA-RISC 7100LC");
                    FamilyList.Add(150, "PA-RISC 7100");
                    FamilyList.Add(160, "V30 Family");
                    FamilyList.Add(176, "Pentium III Xeon Processor");
                    FamilyList.Add(177, "Pentium III Processor with Intel SpeedStep&trade; Technology");
                    FamilyList.Add(178, "Pentium 4");
                    FamilyList.Add(179, "Intel Xeon");
                    FamilyList.Add(180, "AS400 Family");
                    FamilyList.Add(181, "Intel Xeon Processor MP");
                    FamilyList.Add(182, "AMD Athlon XP Family");
                    FamilyList.Add(183, "AMD Athlon MP Family");
                    FamilyList.Add(184, "Intel Itanium 2");
                    FamilyList.Add(185, "Intel Pentium M Processor");
                    FamilyList.Add(190, "K7");
                    FamilyList.Add(200, "IBM390 Family");
                    FamilyList.Add(201, "G4");
                    FamilyList.Add(202, "G5");
                    FamilyList.Add(203, "G6");
                    FamilyList.Add(204, "z/Architecture Base");
                    FamilyList.Add(250, "i860");
                    FamilyList.Add(251, "i960");
                    FamilyList.Add(260, "SH-3");
                    FamilyList.Add(261, "SH-4");
                    FamilyList.Add(280, "ARM");
                    FamilyList.Add(281, "StrongARM");
                    FamilyList.Add(300, "6x86");
                    FamilyList.Add(301, "MediaGX");
                    FamilyList.Add(302, "MII");
                    FamilyList.Add(320, "WinChip");
                    FamilyList.Add(350, "DSP");
                    FamilyList.Add(500, "Video Processor");

                    /* Processor Type */
                    ProcessorTypeList.Add(1, "Other");
                    ProcessorTypeList.Add(2, "Unknown");
                    ProcessorTypeList.Add(3, "Central Processor");
                    ProcessorTypeList.Add(4, "Math Processor");
                    ProcessorTypeList.Add(5, "DSP Processor");
                    ProcessorTypeList.Add(6, "Video Processor");

                    /*Voltage Cap List */
                    VolatageCapList.Add(1, "5 volts");
                    VolatageCapList.Add(2, "3.3 volts");
                    VolatageCapList.Add(4, "2.9 volts");
                }
                catch
                {
                }
            }
            public string GetNumberofCores()
            {
                return GetStringProperty("NumberOfCores");
            }
            public string GetNumberofLogicalProcessor()
            {
                return GetStringProperty("NumberOfLogicalProcessors");
            }
            public string GetManufacturer()
            {
                return GetStringProperty("Manufacturer");
            }
            public string GetDataWidth()
            {
                return GetStringProperty("DataWidth");
            }
            public string GetAddressWidth()
            {
                return GetStringProperty("AddressWidth");
            }
            public string GetArchitecture()
            {
                string temp = "";
                ArchitectureList.TryGetValue(int.Parse(GetStringProperty("Architecture")), out temp);
                return temp;
            }

            public string GetDeviceID()
            {
                return mo.GetPropertyValue("DeviceID").ToString(); 
            }

            public string GetAvailability()
            {
                 string temp = "";
                 AbilityList.TryGetValue(Convert.ToInt32(GetStringProperty("Availability")), out temp);
                 return temp;
            }

            public string GetCaption()
            {
                return GetStringProperty("Caption");
            }

            public string GetCPUStatus()
            {
                string temp = "";
                CpuStatusList.TryGetValue(Convert.ToInt32(GetStringProperty("CpuStatus")), out temp);
                return temp;
            }

            public string GetCurrentClockSpeed()
            {
                return GetStringProperty("CurrentClockSpeed") + " MHz";
            }

            public string GetCurrentVoltage()
            {
                return GetStringProperty("CurrentVoltage") + " volts";
            }

            public string GetDescription()
            {
                return GetStringProperty("Description");
            }

            public string GetExternalClockFreq()
            {
                return GetStringProperty("ExtClock") + " MHz";
            }

            public string GetFamiliy()
            {
                string temp = "";
                try
                {
                    FamilyList.TryGetValue(int.Parse(GetStringProperty("Family")), out temp);
                }
                catch 
                {
                }
                return temp;
            }
            public string GetPowerManagementSupported()
            {
                return GetStringProperty("PowerManagementSupported");
            }

            public string GetName()
            {
                return GetStringProperty("Name");
            }

            public string GetProcessorType()
            {
                string temp = "";
                try
                {
                    ProcessorTypeList.TryGetValue(int.Parse(GetStringProperty("ProcessorType")), out temp);
                }
                catch
                {
                }
                return temp;
            }
            public string GetVoltageCap()
            {
                string temp = "";
                try
                {
                    VolatageCapList.TryGetValue(int.Parse(GetStringProperty("VoltageCaps")), out temp);
                 }
                catch
                {
                    temp = "Voltage Unknown";
                }
                return temp;
            }
            public string GetL2CacheSize()
            {
               return GetStringProperty("L2CacheSize");
            }
            public string GetL2CacheSpeed()
            {
                return GetStringProperty("L2CacheSpeed");
            }
            public string GetL3CacheSize()
            {
                return GetStringProperty("L3CacheSize");
            }

            public string GetL3CacheSpeed()
            {
                return GetStringProperty("L3CacheSpeed");
            }

            public string GetMaxClockSpeed()
            {
                return GetStringProperty("MaxClockSpeed");
            }

            public static List<string> GetAllCPUList()
            {
                List<string> cpunames = new List<string>();
                ManagementObjectSearcher moss = new ManagementObjectSearcher("select * from Win32_Processor");

                foreach (ManagementObject mngo in moss.Get())
                {
                     cpunames.Add(mngo["DeviceID"].ToString());
                     mo  = mngo;
                }
                return cpunames;
            }
        }
    
}
